const MsgMode = require('../common/MsgMode');
const Stype = require("../config/Stype");
const Cmd = require("../config/Cmd");
const ResCode = require("../config/ResCode");
const game_config = require("../config/game_config");

// const hallPro = require("./proto/Hall.proto");
const dt = require('../proto/DT');
const log = require("../utils/log");
const loginRedis = require('./hallRedis');
const redisBase = require("../common/RedisBase");
const db = require("../common/Db");

class hallModel extends MsgMode{

    static getInstance(){
        if(!hallModel.instance){
            hallModel.instance = new hallModel();
            return hallModel.instance;
        }else{
            return hallModel.instance;
        }
    }

    setNewUserData(uid,name,avator){
        let ret = {
            userId: uid,
            name:name,
            avator:avator,
            ballNum:'0',
            ticket:'0',
            ranks:0,
            score:0
        }
        return ret;
    }

    //从数据库查询
    searchInMysql(id){
        log.info('=========responseLoginMsg==========',id);
        return new Promise((resolve,reject)=>{
            log.info('=========responseLoginMsg1==========',id);
            global.loginSerMgr.loginDb.getUserInfo(id).then((result)=>{
                log.warn('查询数据成功',JSON.stringify(result))
                resolve(result);
            }).catch((err)=>{
                log.warn('查询数据失败，失败原因',JSON.stringify(err))
                reject(err);
            })
        })
    }

    //从redis查询是否存在这个用户
    searchInRedis(uid){
        log.info('============searchInRedis==========',uid);

        // global.loginSerMgr.loginRedis.getUserInfo(uid);
        return new Promise((resolve, reject)=>{
            log.info('============searchInRedis1==========',uid);
            global.loginSerMgr.loginRedis.getUserInfo(uid).then((result)=>{
                log.warn('======searchInRedi2=======',result);
                resolve(result);
            }).catch((err)=>{
                log.warn('======searchInRedis3=======',err);
                reject(err);
            })
        })
    }

    //二进制协议解析
    login_ses_success_sinary(user){
        let ret = {
                code:ResCode.Ok,
                userId:user.userId,
                userInfo:{
                userId:user.userId,
                    name:user.name,
                    avator:user.avator,
                    ballNum:user.ballNum.toString(),
                    ticket:user.ticket.toString(),
                    ranks:user.ranks.toString(),
                    score:user.score.toString()
            },
            hallNet:{
                ip:game_config.hall_server.host,
                    port:game_config.hall_server.port
            }
        }
        let buf = loginPro.encodeLoginBackRes(ret);
        log.error('===============解码LoginRes================',loginPro.decodeLoginBackRes(buf));
        let res = this.encodeMsg(Stype.Auth,Cmd.Auth.LoginRes, buf);
        return res;
    }

    //解析json协议
    login_ses_success_json(user){
        let ret = {
            code:ResCode.Ok,
            stype: Stype.Auth,
            ctype: Cmd.Auth.LoginRes,
            buffer: {
                code: ResCode.Ok,
                userId: user.userId,
                userInfo: {
                    userId: user.userId,
                    name: user.name,
                    avator: user.avator,
                    ballNum: user.ballNum.toString(),
                    ticket: user.ticket.toString(),
                    ranks: user.ranks,
                    score: user.score
                },
                hallNet: {
                    ip: game_config.hall_server.host,
                    port: game_config.hall_server.port
                }
            }
        }
        return ret;
    }


    setNewReward(){

    }
}

module.exports = hallModel.getInstance();